package teamgodeater.hicarnet.MVP.Ui.UserInfo;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import java.util.List;

import teamgodeater.hicarnet.Adapter.BaseItem2LineAdapter;
import teamgodeater.hicarnet.Data.BaseItem2LineData;
import teamgodeater.hicarnet.Help.UserDataHelp;
import teamgodeater.hicarnet.R;
import teamgodeater.hicarnet.Widget.RippleBackGroundView;

/**
 * Created by G on 2016/6/14 0014.
 */

public class UserInfoRvAdapter extends BaseItem2LineAdapter {
    public UserInfoRvAdapter(List<? extends BaseItem2LineData> l) {
        super(l);
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == 2) {
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.login_out_button, parent, false);
            return new QuitViewHolder(v);
        } else if (viewType == 1) {
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_info_user_image, parent, false);
            return new HeadImageViewHolder(v);
        }
            return super.onCreateViewHolder(parent, viewType);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder vvholder, final int position) {

        if (getItemViewType(position) == 2) {
            QuitViewHolder holder = (QuitViewHolder) vvholder;
            holder.view.setText("退出登陆");
            holder.view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (itemClickListener != null) {
                        itemClickListener.onClick(null, position);
                    }
                }
            });
        } else if (getItemViewType(position) == 1) {
            HeadImageViewHolder holder = (HeadImageViewHolder) vvholder;
            if (UserDataHelp.headImage == null)
                holder.view.setImageResource(R.drawable.logo);
            else
                holder.view.setImageBitmap(UserDataHelp.headImage);

        } else {
            super.onBindViewHolder(vvholder, position - 1);
        }

    }

    @Override
    public int getItemCount() {
        return super.getItemCount() + 2;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return 1;
        } else if (position == getItemCount() - 1) {
            return 2;
        }else {
            return 0;
        }
    }

    public class QuitViewHolder extends RecyclerView.ViewHolder {
        public RippleBackGroundView view;
        public QuitViewHolder(View itemView) {
            super(itemView);
            this.view = (RippleBackGroundView) itemView.findViewById(R.id.outLoginButton);
        }
    }

    public class HeadImageViewHolder extends RecyclerView.ViewHolder {
        public ImageView view;

        public HeadImageViewHolder(View itemView) {
            super(itemView);
            view = (ImageView) itemView.findViewById(R.id.headImage);
        }
    }
}
